package com.hcj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcj.pojo.Cart;
import com.hcj.pojo.Product;
import com.hcj.service.CartService;
import com.hcj.service.ProductService;
import com.hcj.util.MyResponse;
import com.hcj.util.SelectCart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService service;
    @Autowired
    private ProductService productService;
    @RequestMapping("/add")
    public MyResponse addCart(Cart cart){
        //1.判断订单是否重复
        Cart cart1 = service.selectById(cart);
        if (cart1!=null){
            return new MyResponse("200","订单已存在，添加错误!");
        }
        //给订单添加时间信息
        cart.setCreateTime(new Timestamp(new Date().getTime()));
        //2.查询是否是同一个用户的同一个商品的同一个订单（如果是就进行合并）
        Cart cart2 = service.selectByOne(cart);
        //不同于空，我们就进行对订单数据就行修改
        if (cart2!=null){

            cart2.setQuantity(cart2.getQuantity()+cart.getQuantity());
            //将数据重新修改原订单
            service.updateAll(cart2);
            return new MyResponse("100","插入成功!");
        }
        boolean add = service.add(cart);
        if(add){
            return new MyResponse("100","插入成功!");
        }
        else return new MyResponse("200","插入异常!");
    }
    @RequestMapping("/delete")
    public MyResponse delete(Cart cart){
        //1.判断订单是否存在
        Cart cart1 = service.selectById(cart);
        if (cart1==null){
            return new MyResponse("200","订单不存在");
        }
        boolean delete = service.delete(cart);
        if (delete){
            return new MyResponse("100","删除成功!");
        }
        return new MyResponse("200","删除失败!");
    }
    @RequestMapping("/select/id")
    public Cart selectById(Cart cart){
        return service.selectById(cart);
    }
    @RequestMapping("/select/all")
    public Page<Cart> selectAllPage(Integer currentPage, Integer pageSize){
        return service.selectAllPage(currentPage, pageSize);
    }
    @RequestMapping("/select/uid")
    public Page<Cart> selectAllPgeByUid(Cart cart, Integer currentPage, Integer pageSize){
        currentPage = currentPage == null ? 1 : currentPage;
        pageSize = pageSize == null ? 5 : pageSize;
        if (cart.equals("")) {
            return service.selectAllPage(currentPage, pageSize);
        }
        return service.selectAllPgeByUid(cart.getUid(),currentPage,pageSize);
    }
    @RequestMapping("/select/pid")
    public Page<Cart> selectAllPgeByPid(Cart cart, Integer currentPage, Integer pageSize){
        currentPage = currentPage == null ? 1 : currentPage;
        pageSize = pageSize == null ? 5 : pageSize;
        if (cart.equals("")) {
            return service.selectAllPage(currentPage, pageSize);
        }
        return service.selectAllPgeByPid(cart.getPid(),currentPage,pageSize);
    }
    @RequestMapping("/update")
    public MyResponse update(Cart cart){
        return service.updateAll(cart);
    }
    @RequestMapping("/select/unite")
    public List<SelectCart>  selectUnite(Integer uid){
        //new ArrayList<>()解决空指针异常
        List<SelectCart> selectList = new ArrayList<>();

        Cart cart;
        //通过用户Id查询订单信息
        List<Cart> carts = service.selectByUid(uid);
        if (carts ==null){
            return null;
        }
        for (int i = 0; i < carts.size(); i++) {
            SelectCart selectCart = new SelectCart();
            cart = carts.get(i);
            //通过订单的商品Id查询商品信息
//            System.out.println("cart.getPid() = " + cart);
            Product product = productService.selectOneById(cart.getPid());
//            System.out.println("product = " + product);
            //设置selectCart的属性
            selectCart.setId(cart.getId());
            selectCart.setName(product.getName());
            selectCart.setDetail(product.getDetail());
            selectCart.setQuantity(cart.getQuantity());
            selectCart.setPrice(product.getPrice());
            selectCart.setImgpath(product.getImgpath());
//            System.out.println("selectCart = " + selectCart);

            //把联查对象组装成一个List对象
            selectList.add(i,selectCart);


        }
//        Page<SelectCart> selectCartPage = new Page<>();
//        selectCartPage.setCurrent(currentPage);
//        selectCartPage.setSize(pageSize);
//        System.out.println("selectList = " + selectList);

//        ChangePage changePage = new ChangePage();
//        selectCartPage = changePage.getPages(currentPage, pageSize, selectList);

        return selectList;
    }



}
